package net.ciklum.svnnotify;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

public class RepositoriesActivity extends Activity {
	private List<String> items = new ArrayList<String>();
	private static RepListAdapter adapter = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.repositories);

		items.add("Rep1");
		items.add("Rep2");
		items.add("Rep3");

		adapter = new RepListAdapter(this, items);

		ListView repList = (ListView) findViewById(R.id.rep_list);
		repList.setAdapter(adapter);
		repList.setTextFilterEnabled(true);
		registerForContextMenu(repList);

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.reps_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.menu_add:
			showAddDialog();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

	private void showAddDialog() {
		LayoutInflater inflater = getLayoutInflater();
		View dialoglayout = inflater.inflate(R.layout.repo_dialog, null);
		AlertDialog.Builder dialog = new AlertDialog.Builder(this);
		dialog.setView(dialoglayout)
				.setTitle(R.string.new_rep)
				.setPositiveButton(R.string.save,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();
							}
						})
				.setNegativeButton(R.string.no,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();
							}
						});

		EditText name = (EditText) dialoglayout.findViewById(R.id.name);
		EditText url = (EditText) dialoglayout.findViewById(R.id.url);
		EditText login = (EditText) dialoglayout.findViewById(R.id.login);
		EditText password = (EditText) dialoglayout.findViewById(R.id.password);

		dialog.show();

	}

	private void showEditDialog() {
		LayoutInflater inflater = getLayoutInflater();
		View dialoglayout = inflater.inflate(R.layout.repo_dialog, null);
		AlertDialog.Builder dialog = new AlertDialog.Builder(this);
		dialog.setView(dialoglayout)
				.setTitle(R.string.edit_rep)
				.setPositiveButton(R.string.save,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();
							}
						})
				.setNegativeButton(R.string.no,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();
							}
						});

		EditText name = (EditText) dialoglayout.findViewById(R.id.name);
		EditText url = (EditText) dialoglayout.findViewById(R.id.url);
		EditText login = (EditText) dialoglayout.findViewById(R.id.login);
		EditText password = (EditText) dialoglayout.findViewById(R.id.password);

		dialog.show();

	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.rep_context_menu, menu);
	}

	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
				.getMenuInfo();
		switch (item.getItemId()) {
		case R.id.menu_delete:
			deleteRepository(info.id);
			return true;
		case R.id.menu_edit:
			showEditDialog();
		default:
			return super.onContextItemSelected(item);
		}
	}

	private void deleteRepository(long id) {
		// TODO Auto-generated method stub

	}

	private class RepListWrapper {
		private TextView name = null;
		private TextView url = null;
		private View row = null;

		public RepListWrapper(View row) {
			this.row = row;
		}

		public void populateFrom(String r) {
			getName().setText(r);
			getUrl().setText(Calendar.getInstance().getTime().toString());
		}

		TextView getName() {
			if (name == null) {
				name = (TextView) row.findViewById(R.id.text1);
			}
			return (name);
		}

		TextView getUrl() {
			if (url == null) {
				url = (TextView) row.findViewById(R.id.text2);
			}
			return (url);
		}

	}

	private class RepListAdapter extends ArrayAdapter<String> {

		private Activity activity;
		private List<String> items;

		public RepListAdapter(Activity activity, List<String> items) {
			super(activity, android.R.layout.simple_list_item_1, items);
			this.items = items;
			this.activity = activity;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			View row = convertView;
			RepListWrapper wrapper = null;
			if (row == null) {
				LayoutInflater inflater = activity.getLayoutInflater();
				row = inflater.inflate(R.layout.list_item2, null);
				wrapper = new RepListWrapper(row);
				row.setTag(wrapper);

			} else {
				wrapper = (RepListWrapper) row.getTag();
			}

			wrapper.populateFrom(items.get(position));
			return (row);
		}

	}

}